package com.simple.util.math.function;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @program: simple_tools
 * @description: 数学-一次函数
 * @author: ChenWenLong
 * @create: 2019-10-24 10:48
 **/
public class LinearFunction {

    // 默认对结果都进行四舍五入操作
    // 自变量: independent 因变量: dependent
    // 斜率
    private BigDecimal slope = BigDecimal.ONE;
    //与Y轴的交点
    private BigDecimal point = BigDecimal.ZERO;
    public static LinearFunction instance;
    // 公式 : y=kx+b

    //私有化构造器
    private LinearFunction(){}
    private LinearFunction(BigDecimal slope,BigDecimal point){
        this.point = point;
        this.slope = slope;
    }

    //默认创建一个正比例函数
    static {
        if(instance == null){
            synchronized (LinearFunction.class){
                if(instance == null){
                    instance = new LinearFunction();
                }
            }
        }
    }

    /**
     * 功能描述:
     * 〈创建一次函数〉
     *
     * @params : [slope, point]
     * @return : com.simple.util.math.function.LinearFunction
     * @author : cwl
     * @date : 2019/10/24 11:02
     */
    public static void init(double slope,double point){
        if(slope == 0){
            throw new RuntimeException("slope is not be zero");
        }
        instance.setPoint(new BigDecimal(point));
        instance.setSlope(new BigDecimal(slope));
    }

    /**
     * 功能描述:
     * 〈判断点是否在线上〉
     *
     * @params : [x, y]
     * @return : boolean
     * @author : cwl
     * @date : 2019/10/24 11:03
     */
    public static boolean isOnline(double x ,double y){
        return new BigDecimal(y).setScale(2,RoundingMode.HALF_UP).
                equals(instance.getSlope().multiply(new BigDecimal(x)).add(new BigDecimal(y)).setScale(2,RoundingMode.HALF_UP));
    }

    /**
     * 功能描述:
     * 〈通过y求x的值〉
     *
     * @params : [y]
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 11:35
     */
    public static BigDecimal getX(double y){
        BigDecimal dependent = new BigDecimal(y);
        return (dependent.subtract(instance.getPoint())).divide(instance.getSlope(), 2, RoundingMode.HALF_UP);
    }

    /**
     * 功能描述:
     * 〈根据x求y〉
     *
     * @params : [x]
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 11:44
     */
    public static BigDecimal getY(double x){
        BigDecimal independent = new BigDecimal(x);
        return independent.multiply(instance.slope).add(instance.getPoint()).setScale(2,RoundingMode.HALF_UP);
    }

    /**
     * 功能描述:
     * 〈获得斜率〉
     *
     * @params : [x, y, b]
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 11:57
     */
    public static BigDecimal getSlope(double x,double y,double b){
        BigDecimal dependent = new BigDecimal(y);
        return (dependent.subtract(new BigDecimal(b))).divide(new BigDecimal(x), 2, RoundingMode.HALF_UP);
    }

    /**
     * 功能描述:
     * 〈正比例函数获得斜率〉
     *
     * @params : [x, y]
     * @return : java.math.BigDecimal
     * @author : cwl
     * @date : 2019/10/24 11:58
     */
    public static BigDecimal getSlope(double x,double y){
        BigDecimal dependent = new BigDecimal(y);
        return dependent.divide(new BigDecimal(x), 2, RoundingMode.HALF_UP);
    }


    @Override
    public String toString() {
        return "LinearFunction{" +
                "slope=" + slope +
                ", point=" + point +
                '}';
    }

    private BigDecimal getSlope() {
        return slope;
    }

    private void setSlope(BigDecimal slope) {
        this.slope = slope;
    }

    private BigDecimal getPoint() {
        return point;
    }

    private void setPoint(BigDecimal point) {
        this.point = point;
    }
}
